package com.java.second_goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.second_goods.entity.GoodsImage;
import com.java.second_goods.mapper.GoodsImageMapper;
import com.java.second_goods.service.IGoodsImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 商品图片 服务层实现
 */
@Service
public class GoodsImageServiceImpl extends ServiceImpl<GoodsImageMapper, GoodsImage> implements IGoodsImageService {

    @Autowired
    private GoodsImageMapper goodsImageMapper;

    /**
     * 查询商品图片列表
     *
     * @param goodsId 商品ID
     * @return 图片列表
     */
    @Override
    public List<GoodsImage> selectImagesByGoodsId(Integer goodsId) {
        return goodsImageMapper.selectImagesByGoodsId(goodsId);
    }

    /**
     * 新增商品图片
     *
     * @param goodsImage 商品图片
     * @return 结果
     */
    @Override
    public int insertGoodsImage(GoodsImage goodsImage) {
        if (goodsImage.getStatus() == null) {
            goodsImage.setStatus(1);
        }
        if (goodsImage.getSortOrder() == null) {
            goodsImage.setSortOrder(0);
        }
        if (goodsImage.getIsMain() == null) {
            goodsImage.setIsMain(0);
        }
        return goodsImageMapper.insert(goodsImage);
    }

    /**
     * 批量新增商品图片
     *
     * @param imagesList 图片列表
     * @return 结果
     */
    @Override
    public int batchInsertImages(List<GoodsImage> imagesList) {
        for (GoodsImage image : imagesList) {
            if (image.getStatus() == null) {
                image.setStatus(1);
            }
            if (image.getSortOrder() == null) {
                image.setSortOrder(0);
            }
            if (image.getIsMain() == null) {
                image.setIsMain(0);
            }
        }
        return goodsImageMapper.batchInsertImages(imagesList);
    }

    /**
     * 修改商品图片
     *
     * @param goodsImage 商品图片
     * @return 结果
     */
    @Override
    public int updateGoodsImage(GoodsImage goodsImage) {
        return goodsImageMapper.updateById(goodsImage);
    }

    /**
     * 删除商品图片
     *
     * @param id 图片ID
     * @return 结果
     */
    @Override
    public int deleteGoodsImageById(Integer id) {
        // 逻辑删除，将状态设为0
        GoodsImage goodsImage = new GoodsImage();
        goodsImage.setId(id);
        goodsImage.setStatus(0);
        return goodsImageMapper.updateById(goodsImage);
    }

    /**
     * 批量删除商品图片
     *
     * @param ids 需要删除的图片ID数组
     * @return 结果
     */
    @Override
    public int deleteGoodsImageByIds(Integer[] ids) {
        // 逻辑删除，将状态设为0
        LambdaQueryWrapper<GoodsImage> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(GoodsImage::getId, Arrays.asList(ids));
        GoodsImage goodsImage = new GoodsImage();
        goodsImage.setStatus(0);
        return goodsImageMapper.update(goodsImage, wrapper);
    }

    /**
     * 删除商品的所有图片
     *
     * @param goodsId 商品ID
     * @return 结果
     */
    @Override
    public int deleteImagesByGoodsId(Integer goodsId) {
        return goodsImageMapper.deleteImagesByGoodsId(goodsId);
    }

    /**
     * 设置商品主图
     *
     * @param imageId 图片ID
     * @return 结果
     */
    @Override
    @Transactional
    public int setMainImage(Integer imageId) {
        // 获取当前图片
        GoodsImage currentImage = this.getById(imageId);
        if (currentImage == null) {
            return 0;
        }

        // 将该商品的所有图片设为非主图
        LambdaUpdateWrapper<GoodsImage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(GoodsImage::getGoodsId, currentImage.getGoodsId())
                .set(GoodsImage::getIsMain, 0);
        this.update(updateWrapper);

        // 设置当前图片为主图
        currentImage.setIsMain(1);
        return goodsImageMapper.updateById(currentImage);
    }
}